package com.pda.gongpin.ui.fullinventory;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.pda.gongpin.R;
import com.pda.gongpin.adapter.ImageAdapter;
import com.pda.gongpin.bean.ImageBean;
import com.pda.gongpin.bean.ScanBean;
import com.pda.gongpin.bean.ScanResultBean;
import com.pda.gongpin.bean.ScatteredInfoBean;
import com.pda.gongpin.bean.request.InventoryConfirmRequest;
import com.pda.gongpin.bean.request.QRcodeRequest;
import com.pda.gongpin.contract.FullInventoryDetailsContract;
import com.pda.gongpin.presenter.FullInventoryDetailsPresenter;
import com.pda.gongpin.view.ScanButView;
import com.wangshen.base.app.AppActivityKey;
import com.wangshen.base.glide.GlideEngine;

import com.wangshen.base.net.bean.ImageEntity;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

@Route(path = AppActivityKey.FULLINVENTORYDETAILSACTIVITY)
public class FullInventoryDetailsActivity extends BaseMvpActivity<FullInventoryDetailsPresenter> implements FullInventoryDetailsContract.View {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_back)
    TextView tvBack;
    @BindView(R.id.tv_sku)
    TextView tvSku;
    @BindView(R.id.tv_articleId)
    TextView tvArticleId;
    @BindView(R.id.tv_articleName)
    TextView tvArticleName;
    @BindView(R.id.tv_storageBin)
    TextView tvStorageBin;
    @BindView(R.id.tv_stockNum)
    TextView tvStockNum;
    @BindView(R.id.tv_pallet)
    TextView tvPallet;
    @BindView(R.id.et_actualNum)
    EditText etActualNum;
    @BindView(R.id.rv_image)
    RecyclerView rvImage;
    @BindView(R.id.tv_add)
    ScanButView tvAdd;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    @BindView(R.id.tv_back_storage)
    TextView tvBackStorage;
    private String taskResultId;
    ImageAdapter imageAdapter;
    private List<ImageBean> imageBeans = new ArrayList<>();

    @Override
    public FullInventoryDetailsPresenter getPresenter() {
        return new FullInventoryDetailsPresenter();
    }

    @Override
    public int getView() {
        return R.layout.activity_full_inventory_detail;
    }

    @Override
    public void initView() {
        presenter.getWholePalletInfo(taskResultId);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
        rvImage.setLayoutManager(layoutManager);
        imageBeans.add(new ImageBean(""));
        imageAdapter = new ImageAdapter(R.layout.item_image, imageBeans);
        rvImage.setAdapter(imageAdapter);
        tvBackStorage.setClickable(false);
        tvBackStorage.setFocusable(false);
        tvBackStorage.setBackgroundResource(R.drawable.bg_ededed_4);
        tvBackStorage.setTextColor(getResources().getColor(R.color._4D4D4D));
    }

    @Override
    public void initLinsenterner() {
        tvAdd.setScanCallBack(new ScanButView.ScanCallBack() {
            @Override
            public void callBack(ScanBean scanBean) {
                QRcodeRequest request = new QRcodeRequest();
                request.setQRCode(scanBean.getCode());
                presenter.scanTheQRCodeToVerify(request);
            }
        });
        imageAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                if (view.getId() == R.id.iv_image) {
                    if (TextUtils.isEmpty(imageAdapter.getData().get(position).getPath())) {
                        PictureSelector.create(FullInventoryDetailsActivity.this)
                                .openGallery(PictureMimeType.ofImage())
                                .imageEngine(GlideEngine.createGlideEngine())
                                .maxSelectNum(1)
                                .minSelectNum(1)
                                .isCompress(true)
                                .compressQuality(30)
                                .synOrAsy(true)
                                .imageSpanCount(4)
                                .selectionMode(PictureConfig.MULTIPLE)
                                .forResult(PictureConfig.CHOOSE_REQUEST);
                    } else {
                        if (view.getId()  == R.id.iv_delete){
                            ImageBean imageBean=imageAdapter.getItem(position);
                            imageBeans.remove(imageBean);
                            if (imageBeans.size() == 2){
                                imageBeans.add(0,new ImageBean(""));
                            }
                            imageAdapter.notifyDataSetChanged();
                        }
                    }
                }

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        List<LocalMedia> images;
        if (resultCode == RESULT_OK) {
            if (requestCode == PictureConfig.CHOOSE_REQUEST) {// 图片选择结果回调
                images = PictureSelector.obtainMultipleResult(data);
                presenter.upload(images.get(0).getCompressPath());
              /*  ImageBean imageBean=new ImageBean(images.get(0).getCompressPath());
                imageBeans.add(imageBean);
                imageAdapter.notifyDataSetChanged();*/
            }
        }
    }

    @Override
    public void getData() {
        taskResultId = getIntent().getExtras().getString("taskResultId");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.iv_back, R.id.tv_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finishActivity();
                break;
            case R.id.tv_confirm:
                if (TextUtils.isEmpty(etActualNum.getText())) {
                    showMessage("数量不能为空！");
                    return;
                }
                InventoryConfirmRequest request = new InventoryConfirmRequest();
                request.setTaskResultId(taskResultId);
                request.setActualBoxNum(etActualNum.getText().toString());
                request.setPallet(tvPallet.getText().toString());
                try {
                    request.setPic1Address(imageBeans.get(0).getPath());
                    request.setPic2Address(imageBeans.get(1).getPath());
                    request.setPic3Address(imageBeans.get(2).getPath());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                presenter.wholePalletInventoryConfirm(request);
                break;
        }
    }

    @Override
    public void setData(ScatteredInfoBean scatteredInfoBean) {
        tvArticleId.setText(scatteredInfoBean.getArticleId());
        tvArticleName.setText(scatteredInfoBean.getArticleName());
        tvStockNum.setText(scatteredInfoBean.getStockNum() + "");
        tvStorageBin.setText(scatteredInfoBean.getStorageBinId());
        tvSku.setText(scatteredInfoBean.getSku());
    }

    @Override
    public void setImageEntity(ImageEntity imageEntity) {
        ImageBean imageBean = new ImageBean(imageEntity.getUrl());
        imageBeans.add(imageBean);
        if (imageBeans.size() == 4){
            ImageBean imageBean1=imageBeans.get(0);
            imageBeans.remove(imageBean1);
        }
        imageAdapter.notifyDataSetChanged();
    }

    @Override
    public void setScan(ScanResultBean scan) {
        if (scan.getType().equals("B")) {
            tvPallet.setText(scan.getCode());
        }
    }

    @OnClick(R.id.tv_back_storage)
    public void onViewClicked() {
        presenter.countBackInWarehouse("");
    }
}
